﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生成绩管理系统
{
    public partial class Frmaddstudent : Form
    {
        public Frmaddstudent()
        {
            InitializeComponent();
        }
        private string o_id = null;
        public Frmaddstudent(string id)
        {
            InitializeComponent();
            this.o_id = id;
            try
            {
                DataSet dsClass =StuentInfoOperation.getStuentInfo(new StuentInfoData());
                this.combClass.DataSource = dsClass.Tables[0];
                this.combClass.ValueMember = "所在班级";
                this.combClass.DisplayMember = "所在班级";
            }
            catch(Exception ex)
            {
                ex.ToString();
            }
            if (o_id != "")
            {
                StuentInfoData info = new StuentInfoData();
                info.Sno = id;
                DataSet ds = StuentInfoOperation.getStuentInfo(info);
                this.txtSno.Text = ds.Tables[0].Rows[0]["学号"].ToString();
                this.txtName.Text=ds.Tables[0].Rows[0]["姓名"].ToString();
                this.TxtBirthday.Text = ds.Tables[0].Rows[0]["出生日期"].ToString();
                this.combSex.Text = ds.Tables[0].Rows[0]["性别"].ToString();
                this.txtAddress.Text = ds.Tables[0].Rows[0]["地址"].ToString();
                this.TxtTel.Text = ds.Tables[0].Rows[0]["电话"].ToString();
                this.combClass.SelectedText = ds.Tables[0].Rows[0]["所在班级"].ToString();
                this.txtSno.Enabled = false;
                this.Text = "学生信息修改";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)//学生信息添加
        {
            string sno = this.txtSno.Text;
            string name = this.txtName.Text;
            string birthday = this.TxtBirthday.Text;
            string sex = this.combSex.Text;
            string address = this.txtAddress.Text;
            string tel = this.TxtTel.Text;
            string classid = this.combClass.Text;
            if(sno==null||sno.Trim().Equals(""))
            {//学号不允许为空
                MessageBox.Show("请输入学号！","提示");
                this.txtSno.Focus();
                return;
            }
            if(name==null||name.Trim().Equals(""))
            {//学生姓名不允许为空
                MessageBox.Show("请输入姓名！", "提示");
                this.txtName.Focus();
                return;
            }
            StuentInfoData data = new StuentInfoData();
            data.Sno = sno;
            data.Sname = name;
            data.Birthday = birthday;
            data.Sex = sex;
            data.Address = address;
            data.Tel = tel;
            data.Classid = classid;
            try
            {
                if (o_id == "")
                {
                    if (StuentInfoOperation.insertStuentInfo(data))
                    {
                        MessageBox.Show("添加成功！", "提示");
                        this.txtSno.Text = "";
                        this.txtName.Text = "";
                        this.TxtBirthday.Text = "";
                        this.txtAddress.Text = "";
                        this.TxtTel.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("添加失败！", "错误");
                    }
                }
                else
                {
                    if(StuentInfoOperation.updataStuentInfo(data))
                    {//数据修改
                        MessageBox.Show("修改成功！","提示");
                        //((Frmstuxxcx)this.Parent).bindDataGird();
                    }
                    else
                    {
                        MessageBox.Show("修改失败！","错误");
                    }
                }
            }
          
            catch(Exception ex)
            {
                ex.ToString();
                MessageBox.Show("保存失败！","错误");
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}